//
//  Home_Symptomatic_diet.m
//  Food
//
//  Created by dllo on 15/7/19.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "Home_Symptomatic_diet.h"
#import "BaseHeader.h"
#import "Page_ Diseases.h"
#import "DiseasesFoodController.h"
#import "AFNetworkHandler.h"
#import "DiseasesModel.h"
@interface Home_Symptomatic_diet () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, retain) UISegmentedControl *seg;
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) UIScrollView *scrollView;

@end


@implementation Home_Symptomatic_diet

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"naviBack"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backGround.jpg"]];
    [self createScrollView];
    [self createCollectionView];
    [self getData];
}

- (void) back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void) viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}
#pragma mark - 获得数据
- (void) getData
{
    UIActivityIndicatorView *activityIndicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];
    
    activityIndicator.frame = CGRectMake(0, 0, 100, 100);
    
    activityIndicator.color = [UIColor grayColor];
    
    activityIndicator.center = CGPointMake(ScreenWidth / 2.0, ScreenHeight / 2.0 - 50);
    
    [self.view addSubview:activityIndicator];
    
    [activityIndicator startAnimating];
    NSString *urlStr = [NSString stringWithFormat:@"http://121.41.88.179:80/HandheldKitchen/api/vegetable/tbldisease!getDisease.do?officeId=%@&is_traditional=0", [self.arr[self.seg.selectedSegmentIndex] objectForKey:@"id"]];
    [AFNetworkHandler getWithAFNByURL:urlStr completion:^(id result) {
        
        NSArray *arr = [result objectForKey:@"data"];
        self.diseaseArr = [DiseasesModel arrayWithModelByArray:arr];
        [self.collectionView reloadData];
        [activityIndicator stopAnimating];
    }];
}

- (void) createScrollView
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 35)];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.scrollView.backgroundColor = [UIColor whiteColor];
    
    self.scrollView.showsHorizontalScrollIndicator = NO;
    
    self.scrollView.contentSize = CGSizeMake(80 * self.arr.count, 35);
    
    [self.view addSubview:self.scrollView];
    NSMutableArray *segArr =  [NSMutableArray array];
    for (NSDictionary *dic in self.arr) {
        NSString *name = dic[@"name"];
        [segArr addObject:name];
    }
    self.seg = [[UISegmentedControl alloc] initWithItems:segArr];
    
    self.seg.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backGround.jpg"]];
    
    self.seg.frame = CGRectMake(0, 0, 80 * self.arr.count, 35);
    
    self.seg.tintColor = [UIColor grayColor];
    
    self.seg.selectedSegmentIndex = 0;
    
    [self.seg addTarget:self action:@selector(fun) forControlEvents:UIControlEventValueChanged];
    [self.scrollView addSubview:self.seg];
    
}

- (void) fun
{
    self.navigationItem.title = [self.arr[self.seg.selectedSegmentIndex] objectForKey:@"name"];
    [self getData];
}

#pragma mark - 病科详情collectionView

- (void) createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.itemSize = CGSizeMake((ScreenWidth - 50) / 2, 50);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 40, ScreenWidth - 20, ScreenHeight - 64 - 49 - 40) collectionViewLayout:layout];
    
    self.collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.collectionView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backGround"]];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    self.collectionView.showsVerticalScrollIndicator = NO;
    [self.collectionView registerClass:[Page__Diseases class] forCellWithReuseIdentifier:@"reuse"];
    
    [self.view addSubview:self.collectionView];
}


- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.diseaseArr.count;
}

- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    Page__Diseases *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    cell.diseaseModel = self.diseaseArr[indexPath.item];
    
    return cell;
}

#pragma mark - cell 被选中
- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    DiseasesFoodController *disease = [[DiseasesFoodController alloc] init];
    disease.title = [self.diseaseArr[indexPath.item] diseaseName];
    NSLog(@"model:%@", [self.diseaseArr[indexPath.item] diseaseDescribe]);
    disease.diseaseModel = self.diseaseArr[indexPath.item];
    [self.navigationController pushViewController:disease animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSeg
 
 
 
 ue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
